package cn.apthink.controller;

import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * 响应数据对象集类
 *
 * @author David唐
 * @createdate 2024年1月18日
 * @param <T> 对象类
 */
@EqualsAndHashCode(callSuper = false)
@Data
@ApiModel(value = "响应数据对象集包")
public class ListResult<T> extends BaseResult {
	@ApiModelProperty(name = "datas", value = "返回数据对象集")
	private List<T> data;
	@ApiModelProperty(name = "totalcount", value = "数据对象集的实际总记录数")
	private long totalcount = -1;

	public void setData(List<T> datas) {
		this.data = datas;
		this.totalcount = this.totalcount == -1 ? datas.size() : this.totalcount;
	}

	public final static <T> ListResult<T> success(List<T> data) {
		ListResult<T> result = new ListResult<>();
		result.setErrorid(0);
		result.setMessage("调用成功");
		result.setData(data);
		result.setTotalcount(data.size());
//		result.setTotalcount(Util.isEmpty(data) ? 0 : data.size());
		return result;
	}

	public final static <T> ListResult<T> success(String message, List<T> data) {
		ListResult<T> result = new ListResult<>();
		result.setErrorid(0);
		result.setMessage(message);
		result.setData(data);
		result.setTotalcount(data.size());
//		result.setTotalcount(Util.isEmpty(data) ? 0 : data.size());
		return result;
	}
}
